package com.example.springboot.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.GradeMapper;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.Grade;
import com.example.springboot.domain.po.User;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.GradeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：GradeServiceImpl
 * @Date：2025/2/3 22:39
 * @Filename：GradeServiceImpl
 * @for:
 */
@Service
@Slf4j
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {

       @Resource
       private GradeMapper gradeMapper;

       @Override
       public List<Grade> getAll() {
              List<Grade> list = lambdaQuery()
                      .list();
              return list;
       }


       @Override
       public Grade findById(Integer id) {
              return this.getById(id);
       }

       @Override
       public Grade gradeRegister(Grade grade) {
              //如果班级名为空，抛出异常
              String gradeName = grade.getGradeName();
              if (!ObjectUtil.isNotEmpty(grade)){
                     throw new CustomException(ResultCode.CLASSNAME_ISNULL_ERROR);
              }
              //插入前做班级名唯一性校验
              Grade grade1 = this.selectByGradeName(gradeName);
              if (ObjectUtil.isNotEmpty(grade1)){
                     throw  new CustomException(ResultCode.CLASS_EXIST_ERROR);
              }

              this.save(grade);
              return grade;

       }

       @Override
       public void deleteById(Integer id) {
              this.removeById(id);
       }

       @Override
       public void update(Grade grade) {
              //如果班级名为空，抛出异常
              String gradeName = grade.getGradeName();
              if (ObjectUtil.isEmpty(gradeName)){
                     throw new CustomException(ResultCode.CLASSNAME_ISNULL_ERROR);
              }
              //插入前做班级名唯一性校验
              Grade grade1 = this.selectByGradeName(gradeName);
              //如果修改的班级名与当前正在用的班级名一致，不需要再做唯一性校验
              Grade grade2 = this.getById(grade.getId());
              if (grade2.getGradeName().equals(gradeName)){
                     this.saveOrUpdate(grade);
              }else if (ObjectUtil.isNotEmpty(grade1)){
                     throw  new CustomException(ResultCode.CLASS_EXIST_ERROR);
              }
              this.saveOrUpdate(grade);
       }

       @Override
       public PageInfo<Grade> findPage(Grade search, Integer pageNum, Integer pageSize) {
              Account user = JwtTokenUtils.getCurrentUser();
              if (ObjectUtil.isEmpty(user)) {
                     throw new CustomException(ResultCode.USER_NOT_LOGIN);
              }
              // 开启分页 一定要在操作数据库方法的上面
              PageHelper.startPage(pageNum, pageSize);
              List<Grade> all = this.findByCondition(search);

              return PageInfo.of(all);
       }

       public List<Grade> findByCondition(Grade grade){
              QueryWrapper<Grade> wrapper = new QueryWrapper<Grade>()
                      .like(grade != null && ObjectUtil.isNotEmpty(grade.getGradeName()),"grade_name", grade.getGradeName());
              return gradeMapper.selectList(wrapper);

       }

       public Grade selectByGradeName(String username) {
              QueryWrapper<Grade> queryWrapper = new QueryWrapper<Grade>()
                      .eq("grade_name", username);
              return gradeMapper.selectOne(queryWrapper);
       }

}
